--[[
Description: Task 2.2
Author: DingZY
email: 331646081@qq.com
Date: 2021-06-07 20:05:05
LastEditTime: 2021-06-08 19:23:46
--]]
function Print_Menutitle()
    print("--------------------Menu--------------------")
    print("To choose your choice, please input the number of each choice.")
    print("Choice 1: view all saved data")
    print("Choice 2: save an item data")
    print("Choice 3: read an item data")
    print("Choice 4: delete an item data")
    print("choice 5: exit")
end

function Illegal_Input()
    print("-----Input Error!-----")
    print("please input again.")
end

function Quit_Function()
    local inp, opera
    while 1 do
        print("You can input 0 to return to the main menu")
        inp = io.read()
        opera = tonumber(inp, 10)
        if (opera == 0) then
            break
        end
        Illegal_Input()
    end
end

function View_All_Saved_Data()
    print("----------View----------")
    print("You choose to view all data")
    local sum = 0
    for k in pairs(Tab) do
        sum = sum + 1
    end
    if sum == 0 then
        print("There is no saved data")
    else
        print(string.format("There are(is) %d data.", sum))
    end
    sum = 0
    for k in pairs(Tab) do
        sum = sum + 1
        print(string.format("No.%d The price of a(an) %s is %d", sum, k, Tab[k]))
    end
    Quit_Function()
end

function Save_An_Item_Data()
    print("----------Save----------")
    print("You choose to save data")
    print("Now please input the name of an item:")
    local food, price, str
    food = io.read()
    if (Tab[food] == nil) then
        print("Now please input the price of this item:")
        while true do
            str = io.read()
            price = tonumber(str, 10)
            if price == nil then
                Illegal_Input()
            else
                break
            end
        end
        Tab[food] = price
        print(string.format("You have saved the data: The price of a(an) %s is %d", food, price))
    else
        print("This data already exists")
    end
    Quit_Function()
end

function Read_An_Item_Data()
    print("----------Read----------")
    print("You choose to read data")
    print("Please input the name of the data which you want read:")
    local food
    food = io.read()
    if Tab[food] == nil then
        print("This data doesn't exist")
    else
        print(string.format("The price of a(an) %s is %d", food, Tab[food]))
    end
    Quit_Function()
end

function Delete_An_Item_Data()
    print("----------Delete----------")
    print("You choose to delete a data")
    print("Please input the name of the data which you want to delete:")
    local food
    food = io.read()
    if Tab[food] == nil then
        print(string.format("There is no data of %s", food))
    else
        Tab[food] = nil
        print(string.format("You delete the data of %s", food))
    end
    Quit_Function()
end

function Judge_Menu_Operation(a)
    if (a == 1) then
        View_All_Saved_Data()
    elseif (a == 2) then
        Save_An_Item_Data()
    elseif (a == 3) then
        Read_An_Item_Data()
    elseif (a == 4) then
        Delete_An_Item_Data()
    end
end

Tab = {}
print("Welcome, please choose your choice")
while true do
    Print_Menutitle()
    while 1 do
        local str = io.read()
        Operation = tonumber(str, 10)
        if (Operation == nil) then
            Illegal_Input()
        elseif Operation == 1 or Operation == 2 or Operation == 3 or Operation == 4 or Operation == 5 then
            break
        else
            Illegal_Input()
        end
    end
    if Operation == 5 then
        break
    end
    Judge_Menu_Operation(Operation)
end
